package qx.hyj.petrescue.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import qx.hyj.petrescue.entity.Content;
import qx.hyj.petrescue.mapper.ContentMapper;
import qx.hyj.petrescue.service.contentService;

import java.util.List;

@Service
public class contentServiceImpl extends BaseServiceImpl<ContentMapper, Content, Integer> implements contentService {
    @Resource
    private ContentMapper contentMapper;

    @Override
    public Class<Content> getBaseClazz() {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object getcontent(Integer sendId, Integer receiveId, boolean isRead) {
        List<Content> contents = contentMapper.selectList(Wrappers.<Content>lambdaQuery()
                .eq(Content::getSendId, sendId)
                .eq(Content::getReceiveId, receiveId)
                .eq(!isRead, Content::getIsRead, 0)
                .or()
                .eq(Content::getReceiveId, sendId)
                .eq(Content::getSendId, receiveId)
                .orderByAsc(Content::getCreateTime)
                .eq(!isRead, Content::getIsRead, 3)
        );
        Content content = new Content().builder()
                .isRead(1)
                .build();
        contentMapper.update(content, Wrappers.<Content>lambdaUpdate()
                .eq(Content::getReceiveId, sendId)
                .eq(Content::getSendId, receiveId).eq(Content::getIsRead, 0));
        return contents;
    }

    @Override
    public Object getNewContentNumber(Integer sendId) {
        return contentMapper.getNewContentNumber(sendId);
    }
}
